#include <stdio.h>
#include <stdlib.h>

// 定义单链表节点
struct ListNode {
    int val;
    struct ListNode *next;
};

// 删除链表中重复节点的函数
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if (!head) {
        return head;
    }

    // 创建一个虚拟头节点，简化代码
    struct ListNode* dummy = malloc(sizeof(struct ListNode));
    dummy->next = head;

    struct ListNode* cur = dummy;
    while (cur->next && cur->next->next) {
        if (cur->next->val == cur->next->next->val) {
            // 记录重复值
            int x = cur->next->val;
            // 删除所有重复节点
            while (cur->next && cur->next->val == x) {
                cur->next = cur->next->next;
            }
        } else {
            cur = cur->next;
        }
    }

    return dummy->next;
}

// 创建一个值为val的新节点
struct ListNode* createNode(int val) {
    struct ListNode* newNode = malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

// 打印链表
void printList(struct ListNode* head) {
    while (head != NULL) {
        printf("%d -> ", head->val);
        head = head->next;
    }
    printf("NULL\n");
}

int main() {
    // 创建一个示例链表：1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5
    struct ListNode* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);
    head->next->next->next = createNode(3);
    head->next->next->next->next = createNode(4);
    head->next->next->next->next->next = createNode(4);
    head->next->next->next->next->next->next = createNode(5);

    printf("原始链表：");
    printList(head);

    // 删除重复节点
    head = deleteDuplicates(head);

    printf("删除重复节点后的链表：");
    printList(head);

    return 0;
}
